
package com.epam.android.taining.day7;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

/**
 * Main activity to set up alert time and start/stop service.
 * 
 * @author Natallia_Kukharchyk
 */
public class StartService extends Activity {

  private int alertHour;

  private int alertMinute;

  private TextView alertTime;

  private Button launchService;

  private Button stopService;

  static final int TIME_DIALOG_ID = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // capture our View elements
    alertTime = (TextView) findViewById(R.id.alert_time);
    Button pickTime = (Button) findViewById(R.id.btn_pick_time);

    // add a click listener to the button
    pickTime.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        showDialog(TIME_DIALOG_ID);
      }
    });

    launchService = (Button) findViewById(R.id.btn_launch_service);

    // add a click listener to the button
    launchService.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        Intent myIntent = new Intent(getApplicationContext(), AlertsService.class);
        myIntent.putExtra("hours", alertHour);
        myIntent.putExtra("minutes", alertMinute);
        startService(myIntent);
        launchService.setEnabled(false);
        stopService.setEnabled(true);

      }
    });

    stopService = (Button) findViewById(R.id.btn_stop_service);

    // add a click listener to the button
    stopService.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        Intent myIntent = new Intent(getApplicationContext(), AlertsService.class);
        stopService(myIntent);
        launchService.setEnabled(true);
        stopService.setEnabled(false);

      }
    });
    stopService.setEnabled(false);

    // get the current time
    final Calendar c = Calendar.getInstance();
    alertHour = c.get(Calendar.HOUR_OF_DAY);
    alertMinute = c.get(Calendar.MINUTE);

    // display the current date
    updateDisplay();

  }

  // updates the time we display in the TextView
  private void updateDisplay() {
    alertTime.setText(new StringBuilder().append(pad(alertHour)).append(":").append(pad(alertMinute)));
  }

  private static String pad(int c) {
    if (c >= 10) {
      return String.valueOf(c);
    } else {
      return "0" + String.valueOf(c);
    }
  }

  // the callback received when the user "sets" the time in the dialog
  private final TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
      alertHour = hourOfDay;
      alertMinute = minute;
      updateDisplay();
    }
  };

  @Override
  protected Dialog onCreateDialog(int id) {
    switch (id) {
      case TIME_DIALOG_ID:
        return new TimePickerDialog(this, mTimeSetListener, alertHour, alertMinute, false);
    }
    return null;
  }
}
